﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using WebChatSerivce.Models;
using WebChatService.Models;

namespace WebChatSerivce.Controllers
{
    public class ChatterAPIController : ApiController
    {
       // private WebChatEntities db = new WebChatEntities();
        private IDatabase db;

        public ChatterAPIController()
        {
            db = new ChatterEFDB();
        }

        // GET api/Chatter API
        public IEnumerable<Chatter> GetChatters()
        {
            return (IEnumerable<Chatter>)db.GetAllObjects();
        }

        // GET api/ChatterAPI/5
        public Chatter GetChatter(int id)
        {
            Chatter chatter = (Chatter)db.GetSingleObject(id);
            if (chatter == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return chatter;
        }

        // PUT api/ChatterAPI/5
        public HttpResponseMessage PutChatter(int id, Chatter chatter)
        {
            if (ModelState.IsValid && id == chatter.ID)
            {
                
                db.UpdateObject(chatter);

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/ChatterAPI
        public HttpResponseMessage PostChatter(Chatter chatter)
        {
            if (ModelState.IsValid)
            {
                db.AddObject(chatter);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, chatter);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = chatter.ID }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/ChatterAPI/5
        public HttpResponseMessage DeleteChatter(int id)
        {
            Chatter chatter = (Chatter)db.GetSingleObject(id);
            if (chatter == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.RemoveObject(chatter);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, chatter);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}